<?php

namespace app\system\controller\logic;

use think\Db;
use think\helper\Time;
use think\Request;

class DriverController extends \app\system\controller\BaseController
{


    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * 司机工资计算
     */
    public function index()
    {
        $currentMonth = Time::month();
        $default = date('Y-m-d',$currentMonth[0]).'~'.date('Y-m-d',$currentMonth[1]);
        $rangdate=$this->request->param('rangdate',$default);
        $oil_money=$this->request->param('oil_money',0);
        if(empty($rangdate)){
            $this->error('日期参数错误！');
        }
        $list = $this->calc($rangdate);
        $this->assign('rangdate',$rangdate);
        $this->assign('list',$list);
        $this->assign('oil_money',$oil_money);
        return $this->fetch();
    }
    //导出司机工资
    public function import(){
        $currentMonth = Time::month();
        $default = date('Y-m-d',$currentMonth[0]).'~'.date('Y-m-d',$currentMonth[1]);
        $rangdate=$this->request->param('rangdate',$default);
        $oil_money=$this->request->param('oil_money',0);
        if(empty($rangdate)){
            $this->error('日期参数错误！');
        }
        header("Content-type: application/vnd.ms-excel; charset=utf8");
        header("Content-Disposition: attachment; filename={$rangdate}工资.xls");

        $list = $this->calc($rangdate);
        $this->assign('rangdate',$rangdate);
        $this->assign('list',$list);
        $this->assign('oil_money',$oil_money);
        return $this->fetch();
    }

    private function calc($rangdate){
        $rangdatearr = explode('~',$rangdate);
        $begin = strtotime($rangdatearr[0].' 0:00:00');
        $end = strtotime($rangdatearr[1].' 23:59:59');
        $carModel = cms_moudle(5); //车辆模型
        $orderModel = cms_moudle(14); //订单模型
        $fue = $carModel->column('car_fue','car_sn'); //车辆设定油耗
        //指定区间内得已收车行程单
        $ordercalc = [];
        $orderList = $orderModel->where('back_time','between',[$begin,$end])->where('car_status','>',1)->select();
        if(!$orderList->isEmpty()){
            $orderList = $orderList->toArray();
            foreach ($orderList as $k=>$v){
                if(!empty($v['fleet_driver_ids'])){
                    $fleet_dirver_ids = explode(',',$v['fleet_driver_ids']);//剥离司机
                    $count = count($fleet_dirver_ids);
                    foreach($fleet_dirver_ids as $dirver_id){
                        $ordercalc[$dirver_id]['sum_order_l'] += ($v['order_l']/$count);
                        $ordercalc[$dirver_id]['sum_order_mileage'] += ($v['order_mileage']/$count);
                        $ordercalc[$dirver_id]['car_fue'] += ($v['order_mileage']/$count/100*$fue[$v['car_sn']]);
                    }
                }
            }
        }
        //请假天数
        $leaveList = cms_moudle(12)->where('leave_start_time','>',$begin)->select(); //请假记录
        $leave=[];
        if(!$leaveList->isEmpty()){
            foreach ($leaveList as $k=>$v){
                $leave[$v['leave_uid']] .= implode(',',pr_dates($v['leave_start_time'],$v['leave_end_time'])).',';
            }
        }
        $q1 = pr_dates($rangdatearr[0],$rangdatearr[1]);
        $list = cms_moudle(16)->select()->toArray(); //16 司机列表
        foreach($list as $k=>$v){
            //请假天数
            $q2 = explode(',',rtrim($leave[$v['bind_uid']], ",")); //请假天数
            $list[$k]['leave_arr']=array_intersect($q1,$q2);
            if($ordercalc[$v['aid']]['sum_order_l']){
                $list[$k]['sum_order_l'] = $ordercalc[$v['aid']]['sum_order_l'];
            }else{
                $list[$k]['sum_order_l'] = 0;
            }
            if($ordercalc[$v['aid']]['sum_order_mileage']){
                $list[$k]['sum_order_mileage'] = $ordercalc[$v['aid']]['sum_order_mileage'];
            }else{
                $list[$k]['sum_order_mileage'] = 0;
            }
            if($ordercalc[$v['aid']]['car_fue']){
                $list[$k]['car_fue'] = $ordercalc[$v['aid']]['car_fue'];
            }else{
                $list[$k]['car_fue'] = 0;
            }
        }
        return $list;
    }
}
